package com.jonluo.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.jonluo.mapper.SysMenuMapper;
import com.jonluo.model.Example;
import com.jonluo.model.SysMenu;
import com.jonluo.service.SysMenuService;
import com.jonluo.utils.PageInfo;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.cache.annotation.Caching;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

/**
 * @author jonluo
 * Date 2017/9/15
 */
@Service("sysMenuService")
public class SysMenuServiceImpl implements SysMenuService {

    @Autowired
    private SysMenuMapper sysMenuMapper;

    @Override
    @Transactional
    @Caching(evict = {@CacheEvict(value = {"menuList", "menuCount","menuSubMenu","menuAllMenu"}, allEntries = true)})
    public int save(SysMenu sysMenu) {
        return sysMenuMapper.insertSelective(sysMenu);
    }

    @Override
    @Transactional
    @Caching(evict = {@CacheEvict(value = {"menuList", "menuCount","menuSubMenu","menuAllMenu"}, allEntries = true)})
    public int saveBatch(List<SysMenu> list) {
        int i = 0;
        for (SysMenu sysMenu : list){
            i += save(sysMenu);
        }
        return i;
    }

    @Override
    @Transactional
    @Caching(evict = {@CacheEvict(value = {"menuList", "menuCount","menuSubMenu","menuAllMenu"}, allEntries = true),
            @CacheEvict(value = "menu", key = "'menuId'+#p0.menuId")})
    public int update(SysMenu sysMenu) {
        return sysMenuMapper.updateByPrimaryKey(sysMenu);
    }

    @Override
    @Transactional
    @Caching(evict = {@CacheEvict(value = {"menuList", "menuCount","menuSubMenu","menuAllMenu"}, allEntries = true),
            @CacheEvict(value = "menu", key = "'menuId'+#p0")})
    public int delete(String id) {
        return sysMenuMapper.deleteByPrimaryKey(id);
    }

    @Override
    @Transactional
    @Caching(evict = {@CacheEvict(value = {"menuList", "menuCount","menu","menuSubMenu","menuAllMenu"}, allEntries = true)})
    public int deleteBatch(String[] ids) {
        Example example = new Example();
        Example.Criteria criteria = example.createCriteria();
        criteria.andIn("menuId", Arrays.asList(ids));
        return sysMenuMapper.deleteByExample(example);
    }

    @Override
    @Cacheable(value = "menu", key = "'menuId'+#p0")
    public SysMenu query(String id) {
        return sysMenuMapper.selectByPrimaryKey(id);
    }

    @Override
    @Cacheable(value = "menuList", key = "'menuList'+#p0.toString()")
    public List<SysMenu> queryList(PageInfo pageInfo) {
        return sysMenuMapper.selectByExample(pageInfo2Example(pageInfo));
    }

    @Override
    @Cacheable(value = "menuCount", key = "'menuCount'+#p0.toString()")
    public int queryTotal(PageInfo pageInfo) {
        return sysMenuMapper.countByExample(pageInfo2Example(pageInfo));
    }

    @Override
    @Cacheable(value = "menuCount", key = "'menuCount'")
    public int queryTotal() {
        return sysMenuMapper.countByExample(null);
    }

    @Override
    @Cacheable(value = "menuAllMenu", key = "'menuAllMenu'+#p0+'-'+#p1")
    public List<SysMenu> queryAllMenuList(String parentId,List<String> menuIdList){
        List<SysMenu> menuList = querySubMenuList(parentId,menuIdList);
        //递归获取子菜单
        getMenuTreeList(menuList, menuIdList);
        return menuList;
    }

    /**
     * 获取下级菜单列表
     */
    @Override
    @Cacheable(value = "menuSubMenu", key = "'menuSubMenu'+#p0+'-'+#p1")
    public List<SysMenu> querySubMenuList(String parentId,List<String> menuIdList) {
        Example example = new Example();
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("parentId", parentId);
        example.setOrderByClause(" order_num asc ");

        List<SysMenu> menuList = sysMenuMapper.selectByExample(example);
        if(menuIdList == null){
            return menuList;
        }
        List<SysMenu> userMenuList = new ArrayList<SysMenu>();
        for(SysMenu menu : menuList){
            if(menuIdList.contains(menu.getMenuId())){
                userMenuList.add(menu);
            }
        }
        return userMenuList;
    }

    /**
     *
     * @param pageInfo
     * 分页参数转为条件封装example
     * @return example
     */
    private  Example pageInfo2Example(PageInfo pageInfo){
        Example example = new Example();
        example.setOrderByClause(" order_num asc ");
        Example.Criteria criteria = example.createCriteria();
        //分页
        if (pageInfo.getOffset()!= 0 &&  pageInfo.getOffset()!= 0){
            example.setOffset(pageInfo.getOffset());
            example.setLimit(pageInfo.getLimit());
        }
        //查询条件
        JSONObject json = pageInfo.getJsonParams();
        if (StringUtils.isNotBlank(json.getString("type"))){
            criteria.andIn("type",Arrays.asList(json.getString("type").split(",")));
        }

        return example;
    }



    /**
     * type = 0 为目录
     * 递归
     */
    private List<SysMenu> getMenuTreeList(List<SysMenu> menuList, List<String> menuIdList){
        List<SysMenu> subMenuList = new ArrayList<SysMenu>();
        for(SysMenu entity : menuList){
            if(entity.getType() == 0){//目录
                entity.setList(getMenuTreeList(querySubMenuList(entity.getMenuId(), menuIdList), menuIdList));
            }
            subMenuList.add(entity);
        }
        return subMenuList;
    }

}
